<?php


namespace App\Http\Controllers\Customer\Entryorder;


use App\Http\Controllers\Controller;
use App\Http\Requests\Customer\Entryorder\StoreRequest;
use App\Http\Requests\Customer\Entryorder\UpdateRequest;
use App\Http\Searchs\Customer\Entryorder\IndexSearch;
use App\Repositories\Entryorder;
use Illuminate\Support\Facades\DB;

class IndexController extends Controller
{
    /**
     * @var Entryorder
     * @author luffyzhao@vip.126.com
     */
    private $entryorder;

    public function __construct(Entryorder $entryorder)
    {
        $this->entryorder = $entryorder;
    }

    /**
     * @param IndexSearch $search
     * @return \Illuminate\Http\JsonResponse
     * @author luffyzhao@vip.126.com
     */
    public function index(IndexSearch $search)
    {
        return $this->response(
            $this->entryorder->simplePaginate($search->toArray())
        );
    }

    /**
     * @param StoreRequest $request
     * @return \Illuminate\Http\JsonResponse
     * @author luffyzhao@vip.126.com
     */
    public function store(StoreRequest $request)
    {
        return $this->response(
            DB::transaction(function () use ($request) {
                $this->entryorder->create($request->only([
                    'foreign', 'customer_id', 'expect_at', 'products',
                ]));
            })
        );
    }

    /**
     * @param $id
     * @return \Illuminate\Http\JsonResponse
     * @author luffyzhao@vip.126.com
     */
    public function edit($id)
    {
        return $this->response(
            $this->entryorder->edit($id)
        );
    }

    /**
     * @param UpdateRequest $request
     * @return \Illuminate\Http\JsonResponse
     * @throws \Throwable
     * @author luffyzhao@vip.126.com
     */
    public function update(UpdateRequest $request, $id)
    {
        return $this->response(
            DB::transaction(function () use ($id, $request) {
                $this->entryorder->update($id, $request->only([
                    'foreign', 'customer_id', 'expect_at', 'products',
                ]));
            })
        );
    }

    /**
     * @param $id
     * @return \Illuminate\Http\JsonResponse
     * @author luffyzhao@vip.126.com
     */
    public function affirm($id){
        return $this->response(
            DB::transaction(function () use ($id) {
                $this->entryorder->affirm($id);
            })
        );
    }
}
